package com.wilmer.consultants.service.impl;

import java.util.*;

import com.wilmer.common.utils.DateUtils;
import com.wilmer.common.utils.SecurityUtils;
import com.wilmer.consultants.domain.ConsultantsVo;
import com.wilmer.consultants_order.mapper.BankConsultantsOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wilmer.consultants.mapper.BankFinancialConsultantsMapper;
import com.wilmer.consultants.domain.BankFinancialConsultants;
import com.wilmer.consultants.service.IBankFinancialConsultantsService;

import javax.annotation.Resource;

/**
 * 金融顾问Service业务层处理
 * 
 * @author hans
 * @date 2024-04-01
 */
@Service
public class BankFinancialConsultantsServiceImpl implements IBankFinancialConsultantsService 
{
    @Resource
    private BankFinancialConsultantsMapper bankFinancialConsultantsMapper;
    @Resource
    private BankConsultantsOrderMapper bankConsultantsOrderMapper;

    /**
     * 查询金融顾问
     * 
     * @param id 金融顾问主键
     * @return 金融顾问
     */
    @Override
    public BankFinancialConsultants selectBankFinancialConsultantsById(Long id)
    {
        return bankFinancialConsultantsMapper.selectBankFinancialConsultantsById(id);
    }

    /**
     * 查询金融顾问列表
     * 
     * @param bankFinancialConsultants 金融顾问
     * @return 金融顾问
     */
    @Override
    public List<BankFinancialConsultants> selectBankFinancialConsultantsList(BankFinancialConsultants bankFinancialConsultants)
    {
        return bankFinancialConsultantsMapper.selectBankFinancialConsultantsList(bankFinancialConsultants);
    }

    @Override
    public List<ConsultantsVo> getList(ConsultantsVo consultantsVo) {
        List<ConsultantsVo> returnList=new ArrayList<>();
        // 本页顾问集合
        List<ConsultantsVo> consultantsList = bankFinancialConsultantsMapper.getList(consultantsVo);
        // 查出用户已经付费的订单中顾问的id
        List<Long> conIds = bankConsultantsOrderMapper.getConId(SecurityUtils.getLoginUser().getUser().getUserId());
        System.out.println(SecurityUtils.getLoginUser().getUser().getUserId());
        // 将已支付顾问的ID存储在HashSet中以提高查找效率
        System.out.println(conIds);
        // 如果conIds不为空，则检查并标记已支付的顾问
        if (conIds != null && !conIds.isEmpty()){
            Set<Long> paidConsultantIdsSet = new HashSet<>(conIds);
            System.out.println(paidConsultantIdsSet);
            // 遍历consultantsList并设置pay属性
            for (ConsultantsVo vo : consultantsList) {
                if (paidConsultantIdsSet.contains(vo.getId())) {
                    vo.setPay(true);
                }
                // 添加到返回列表中
                returnList.add(vo);
            }
            return returnList;
        }
        return consultantsList;
    }

    /**
     * 新增金融顾问
     * 
     * @param bankFinancialConsultants 金融顾问
     * @return 结果
     */
    @Override
    public int insertBankFinancialConsultants(BankFinancialConsultants bankFinancialConsultants)
    {
        bankFinancialConsultants.setCreateTime(DateUtils.getNowDate());
        return bankFinancialConsultantsMapper.insertBankFinancialConsultants(bankFinancialConsultants);
    }

    /**
     * 修改金融顾问
     * 
     * @param bankFinancialConsultants 金融顾问
     * @return 结果
     */
    @Override
    public int updateBankFinancialConsultants(BankFinancialConsultants bankFinancialConsultants)
    {
        bankFinancialConsultants.setUpdateTime(DateUtils.getNowDate());
        return bankFinancialConsultantsMapper.updateBankFinancialConsultants(bankFinancialConsultants);
    }

    /**
     * 批量删除金融顾问
     * 
     * @param ids 需要删除的金融顾问主键
     * @return 结果
     */
    @Override
    public int deleteBankFinancialConsultantsByIds(Long[] ids)
    {
        return bankFinancialConsultantsMapper.deleteBankFinancialConsultantsByIds(ids);
    }

    /**
     * 删除金融顾问信息
     * 
     * @param id 金融顾问主键
     * @return 结果
     */
    @Override
    public int deleteBankFinancialConsultantsById(Long id)
    {
        return bankFinancialConsultantsMapper.deleteBankFinancialConsultantsById(id);
    }
}
